package com.clstu.collections_;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Collections_ {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("hello !!");
        list.add("world ");
        list.add("world ");
        list.add("world ");
        list.add("world ");
        list.add("i'm ");
        list.add("your ");
        list.add("father");
        System.out.println(list);
        Collections.reverse(list);//Collections.reverse()，逆序
        System.out.println(list);
        for (int i=0;i<5;i++) {
            Collections.shuffle(list);//随机排序
            System.out.println(list);
        }
        System.out.println("==============");
        Collections.sort(list);
        System.out.println(list);
        Collections.sort(list, new Comparator() {//安装比较器排序
            @Override
            public int compare(Object o1, Object o2) {
                return ((String)o2).compareTo((String)o1);
            }
        });
        System.out.println(list);
        System.out.println("==========");
        Collections.swap(list,0,1);//交换这位置的元素
        System.out.println(list);
        Object max=Collections.max(list, new Comparator() {//根据比较器找到最大值
            @Override
            public int compare(Object o1, Object o2) {
                return ((String)o1).length()-((String)o2).length();
            }
        });
        System.out.println("max= "+max);
        System.out.println(Collections.max(list));//输出默认比较器比较出来的最大值
        //Collections.min()用法与max一样
        System.out.println(Collections.frequency(list,"world "));//得到这个元素在列表中出现的次数
        List list1=new ArrayList();
        //Collections.copy(list1,list);//IndexOutOfBoundsException，因为list1当前的size为0
        //System.out.println(list1);//
        for (int i = 0; i < list.size(); i++) {
            list1.add("");
        }
        Collections.copy(list1,list);//目的列表的size()必须大于等于源list的size()
        System.out.println(list1);
        System.out.println(list);
        System.out.println("===============");
        Collections.replaceAll(list,"world ","heihei");//替换掉所有的world ，为嘿嘿
        System.out.println(list);
    }
}
